package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Client;
import com.pojo.Distribution;
import com.pojo.JdInventory;
import com.pojo.Order;
import com.service.ClientService;
import com.service.DistributionService;
import com.service.InventoryService;
import com.service.OrderService;
import com.vo.QueryVo;
import com.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping ("distribution")
@ResponseBody
public class DistributionController {
    
    @Resource
    private DistributionService distributionService;
    @Resource
    private ClientService clientService;
    @Resource
    private InventoryService inventoryService;
    @Resource
    private OrderService orderService;
    
    @RequestMapping (value = "list", method = RequestMethod.GET)
    public ResultVo<Distribution> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Distribution> distributionPageInfo = distributionService.queryByPage(pageNum , pageSize , vo);
        return new ResultVo<>(distributionPageInfo);
    }
    
    @RequestMapping (value = "{d_num}", method = RequestMethod.DELETE)
    public ResultVo<Distribution> del(@PathVariable("d_num") Integer d_num) {
        int i = distributionService.delByKey(d_num);
        if (i == 0) {
            return new ResultVo<>(500 , "该信息已被删除！");
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "" , method = RequestMethod.POST)
    public ResultVo<Distribution> add(Distribution distribution){
    
        
        Client c = new Client();
        c.setC_name(distribution.getC_name());
        List<Client> clients = clientService.queryByType(c);
        if (clients.isEmpty()){
            return new ResultVo<>(500,"客户名称不存在！");
        }else {
            distribution.setC_id(clients.get(0).getC_id());
        }
        
        int i = distributionService.addSelective(distribution);
        if (i == 0){
            return new ResultVo<>(500 , "注意填写信息！");
        }
    
        //出库后 ，更变库存信息
        //通过 订单 获取 指定商品
        /*List<Order> orders = orderService.queryByType(new Order(distribution.getO_id()));
        
        if(orders.isEmpty()){
            return new ResultVo<>(500 , "订单号不存在");
        }
        int update = 0;
        for (Order order : orders) {
            JdInventory jdInventory = new JdInventory();
            jdInventory.setP_id(order.getP_id());
            List<JdInventory> inventories = inventoryService.queryByType(jdInventory);
            JdInventory inventory = inventories.get(0);
            inventory.setI_outNum(inventory.getI_outNum()+1);
            inventory.setI_InventoryNum(inventory.getI_InventoryNum()-1);
            update += inventoryService.update(inventory);
        }
        if ( update <= 0) {
            return new ResultVo<>(500 , "更改异常！");
        }*/
        return new ResultVo<>();
    }
    
    @RequestMapping(value = "{d_num}" , method = RequestMethod.GET)
    public ResultVo<Distribution> queryBykey(@PathVariable("d_num") Integer d_num) {
        Distribution distribution = distributionService.queryByKey(d_num);
        Client c = new Client();
        Client client = null;
        c.setC_id(distribution.getC_id());
        List<Client> clients = clientService.queryByType(c);
        if (!clients.isEmpty()) {
            client = clients.get(0);
        }
        distribution.setC_name(client.getC_name());
        return new ResultVo<>(distribution);
    }
    
    
    @RequestMapping(value = "" , method = RequestMethod.PUT)
    public ResultVo<Distribution> update(Distribution distribution){
    
        Client c = new Client();
        Client client = null;
        c.setC_name(distribution.getC_name());
        List<Client> clients = clientService.queryByType(c);
        if (!clients.isEmpty()) {
            client = clients.get(0);
        }else{
            return new ResultVo<>(500,"填写名称有误！");
        }
        distribution.setC_id(client.getC_id());
        
        int update = distributionService.update(distribution);
        if (update == 0) {
            return new ResultVo<>(500,"服务器异常！");
        }
        return new ResultVo<>();
    }
}
